package com.wmh.baseservice.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.admin.pojo.AdRolePermissions;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.common.redis.config.RedisConfig;
import com.wmh.baseservice.common.redis.utils.RedisUtil;
import com.wmh.baseservice.common.shiro.config.ShiroConfig;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.merchant.mapper.MerchantRolePermissionsMapper;
import com.wmh.baseservice.merchant.pojo.MerchantRolePermissions;
import com.wmh.baseservice.merchant.service.MerchantRolePermissionsService;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wmh generator
 * @since 2021-12-24
 */
@Service
public class MerchantRolePermissionsServiceImpl extends CommonServiceImpl<MerchantRolePermissionsMapper, MerchantRolePermissions> implements MerchantRolePermissionsService {


    @Resource
    private MerchantRolePermissionsService merchantRolePermissionsService;
    @Resource
    private RedisUtil redisUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPer(Long id, List<MerchantRolePermissions> merchantRolePermissions) {
        //删除该账号原权限
        List<Long> list = merchantRolePermissionsService.list(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.MERCHANT_ID, id).eq(MerchantRolePermissions.TYPE, 2)).stream().map(MerchantRolePermissions::getId).collect(Collectors.toList());
        if (list.size() != 0) {
            if (!merchantRolePermissionsService.removeByIds(list)) {
                throw new ServiceException();
            }
        }
        if (merchantRolePermissions.size() != 0) {
            //添加该账号权限
            if (!merchantRolePermissionsService.saveBatch(merchantRolePermissions)) {
                throw new ServiceException();
            }
        }
        //更新redis
        String project = RedisConfig.KEY_INFO;
        String key = "role_permissions_" + id + ShiroConfig.SHIRO_MERCHANT + project;
        try {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            SimpleAuthorizationInfo infoRaw = (SimpleAuthorizationInfo) redisUtil.get(key);
            info.setRoles(CommUtil.checkNull(infoRaw) ? infoRaw.getRoles() : null);
            Set<String> permissions = merchantRolePermissions.size() == 0 ? new HashSet<>() : merchantRolePermissions.stream().filter(item -> CommUtil.checkNull(item.getPermissions())).map(MerchantRolePermissions::getPermissions).collect(Collectors.toSet());
            info.setStringPermissions(permissions);
            redisUtil.set(key, info);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error("redis角色权限更新错误,key是:" + key + "。原因是:" + e.getMessage());
        }
    }
}
